$(function (){
    $('.confirm-btn').confirmation({
        animation: true,
        placement: "top",
        title: "确定要删除吗？",
        btnOkLabel: '确定',
        btnCancelLabel: '取消',
        onConfirm: function () {
            doDelete($(this).attr("attr-id"));
        },
        onCancel: function () {
        }
    })
});

/**
 * 删除产品
 * @param id 产品id
 */
function doDelete(id){
    $.ajax({
        url: bathPath_+'/delete/'+id,
        type: 'get',
        timeout: 30000,
        beforeSend: function () {
        },
        success: function (result) {
            if (0 == result.error) {
                $("#tr_"+id).remove();
            }else{
                alert(result.msg);
            }
        }
    });
}

/**
 * 跳出上传弹框
 * @param fileId
 * @param prodName
 */
function doUpload(fileId,prodName){
    //alert(fileId+"-"+prodName);
    $("#prodName").val(prodName);
    $("#prodId").val(fileId);

    $('#uploadModal').modal({
        keyboard: false
    })
}

/**
 * 开始文件上传
 */
function uploadSend(){
    $('#uploadModal').modal('hide');
    $('#loadingModal').modal({
        keyboard: false
    })

    setTimeout(function (){
        var formData=new FormData($('#uploadForm')[0]);
        $.ajax({
            type:'POST',
            data:formData,
            url:bathPath_+'/files/upload/'+$("#prodId").val(),
            processData: false,
            contentType:false,
            async:false,
            success:function (result){
                alert(result.msg);
                $('#loadingModal').modal('hide');
                setTimeout(function (){
                    location.reload();
                },1000);
            }
        })
    },1000);
}

/**
 * 打开新增分组弹窗
 */
function addGroup(){
    $("#id").val("");
    $("#groupName").val("");
    $("#warnTips").html("").hide();

    $('#prodGroupModal').modal({
        keyboard: false
    });

}

/**
 * 打开修改分组弹窗
 */
function editGroup(id,name){
    $("#groupName").val(name);
    $("#id").val(id);
    $("#warnTips").html("").hide();
    $('#prodGroupModal').modal({
        keyboard: false
    });
}


/**
 * 添加/修改产品分组
 */
function doUpdateGroup(){
    if ("" == $("#groupName").val()) {
        $(".alert").hide();
        $("#warnTips").html("请输入产品名称").show();
        return;
    }
    $('#prodGroupModal').modal('hide');
    $('#loadingModal').modal({
        keyboard: false
    });
    $('#loadingModal').modal({
        keyboard: false
    });

    var url=$("#id").val()==""?bathPath_+'/prod_group/do_create':bathPath_+'/prod_group/do_edit';
    setTimeout(function (){
        $.ajax({
            type:'POST',
            data:$("#groupForm").serialize(),
            url:url,
            success:function (result){
                alert(result.msg);
                $('#loadingModal').modal('hide');
                setTimeout(function (){
                    location.reload();
                },1000);
            }
        })
    },1000);
}

